﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;


public partial class Main_Inventory_DeliveryAddress : System.Web.UI.Page
{
    Datautility du = new Datautility();
    string sql = "";
    string customerid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if ((Request.QueryString["customerid"].ToString()) != null)
        {
            customerid = Request.QueryString["customerid"].ToString();
            hid1.Value = Request.QueryString["control1"].ToString();
            hid2.Value = Request.QueryString["control2"].ToString();
            hiddelivery.Value = Request.QueryString["addid"].ToString();
            if (!IsPostBack)
            {
                bindstate();
                DeliveryNameBind();
                BindGridview();
            }
        }
    }
        
    protected void DeliveryNameBind()
    {
        DDLDeliveryName.Items.Add(new ListItem("M/s", "0"));
        DDLDeliveryName.Items.Add(new ListItem("Mr.", "1"));
        DDLDeliveryName.Items.Add(new ListItem("Mrs.", "2"));
        DDLDeliveryName.Items.Add(new ListItem("Ms.", "3"));
       
    }
    protected void bindstate()
    {
        sql = "select * from MAS_State";
        DataTable DT = du.getdata(sql);
        DDLDileveryState.DataSource = DT;
        DDLDileveryState.DataTextField = "StateName";
        DDLDileveryState.DataValueField = "StateId";
        DDLDileveryState.DataBind();
        DDLDileveryState.Items.Insert(0, new ListItem("---Select State---", "0"));
        DDLDileveryState.SelectedValue = "0";
    }
    protected void BindGridview()
    {
        string sql = "select DELIVERYADDID, LID, Title+' '+CONTACTNAME as Name, ADD1+','+ADD2+','+ADD3+','+ADD4 as  Address, Country, Pincode from ACC_LEDDELIVERYADDDETAILS where LID=" + customerid;
        DataTable dt = du.getdata(sql);
        grddelivery.DataSource = dt;
        grddelivery.DataBind();
        
    }
    protected void Savedeliveryadress()
    {
        try
        {
            string Lstdate = string.Empty;
            string cstdate = string.Empty;
            string cepdate = string.Empty;
            if (txtDeliveryVatDate.Text.ToString() != string.Empty)
            {
                string[] vat = txtDeliveryVatDate.Text.ToString().Split('/');
                Lstdate = vat[1].ToString() + "/" + vat[0].ToString() + "/" + vat[2].ToString();
            }

            if (txtDeliveryInterStateDate.Text.ToString() != string.Empty)
            {
                string[] Interstate = txtDeliveryInterStateDate.Text.ToString().Split('/');
                cstdate = Interstate[1].ToString() + "/" + Interstate[0].ToString() + "/" + Interstate[2].ToString();
            }
            if (txtDeliveryCENVATDate.Text.ToString() != string.Empty)
            {
                string[] Cenvat = txtDeliveryCENVATDate.Text.ToString().Split('/');
                cepdate = Cenvat[1].ToString() + "/" + Cenvat[0].ToString() + "/" + Cenvat[2].ToString();
            }

            SqlParameter[] parm = {
                                  //new SqlParameter("@DELIVERYADDID",""),
                                  new SqlParameter("@LID",customerid),
                                  new SqlParameter("@TITLE",DDLDeliveryName.SelectedItem.Text.Trim()),
                                  new SqlParameter("@CONTACTNAME",txtDeliveryName.Text.Trim()),
                                  new SqlParameter("@ADD1",txtDileveryAddress1.Text.Trim()),
                                  new SqlParameter("@ADD2",txtDileveryAddress2.Text.Trim()),
                                  new SqlParameter("@ADD3",txtDileveryAddress3.Text.Trim()),
                                  new SqlParameter("@ADD4",txtDileveryAddress4.Text.Trim()),
                                  new SqlParameter("@PINCODE",txtDileveryPincode.Text.Trim()),
                                  new SqlParameter("@STATEID",DDLDileveryState.SelectedValue),
                                  new SqlParameter("@COUNTRY",DDLDileveryCountry.SelectedItem.Text.Trim()),
                                  new SqlParameter("@LSTNUM",txtVat.Text.Trim()),
                                  new SqlParameter("@LSTDATE",Lstdate),
                                  new SqlParameter("@CSTNUM",txtDeliveryInterState.Text.Trim()),
                                  new SqlParameter("@CSTDATE",cstdate),
                                  new SqlParameter("@CEPANUM",txtDeliveryCENVAT.Text.Trim()),
                                  new SqlParameter("@CEPADATE",cepdate),
                                  new SqlParameter("@CName",Session["username"].ToString())
                              };
            du.executeprocedure("USP_ACC_SaveLedgerDeliveryAddressDetails", parm);
            //du.executeprocedure("usp_insert_ACC_LEDDELIVERYADDDETAILS", parm);
            lblMsg.Text = ("Record Insert Succesfully");
            BindGridview();
        }

        catch (Exception e)
        {
            lblMsg.Text = e.Message;
            //lblMsg.Text=("Error to insert Successfuly");
        }
    }

    protected void UpdateDeliveryDetails()
    {

        try
        {
                string LstDate = "";
                string CSTDate = "";
                string CepDate = "";

                if (txtDeliveryVatDate.Text != "")
                {
                    string[] Lst = txtDeliveryVatDate.Text.Split(new char[] { '/' });
                    LstDate = Lst[1].ToString() + "/" + Lst[0].ToString() + "/" + Lst[2].ToString();
                }

                if (txtDeliveryInterStateDate.Text != "")
                {
                    string[] CST = txtDeliveryInterStateDate.Text.Split(new char[] { '/' });
                    CSTDate = CST[1].ToString() + "/" + CST[0].ToString() + "/" + CST[2].ToString();
                }
                if (txtDeliveryCENVATDate.Text != "")
                {
                    string[] Cep = txtDeliveryCENVATDate.Text.Split(new char[] { '/' });
                    CepDate = Cep[1].ToString() + "/" + Cep[0].ToString() + "/" + Cep[2].ToString();
                }

                SqlParameter[] sparam = 
                                        {
                                           
                                             new SqlParameter("@LID",customerid),
                                            new SqlParameter("@TITLE",DDLDeliveryName.SelectedItem.Text.Trim()),
                                            new SqlParameter("@CONTACTNAME",txtDeliveryName.Text.Trim()),
                                            new SqlParameter("@ADD1",txtDileveryAddress1.Text.Trim()),
                                            new SqlParameter("@ADD2",txtDileveryAddress2.Text.Trim()),
                                            new SqlParameter("@ADD3",txtDileveryAddress3.Text.Trim()),
                                            new SqlParameter("@ADD4",txtDileveryAddress4.Text.Trim()),
                                            new SqlParameter("@PINCODE",txtDileveryPincode.Text.Trim()),
                                            new SqlParameter("@STATEID",DDLDileveryState.SelectedValue),
                                            new SqlParameter("@COUNTRY",DDLDileveryCountry.SelectedItem.Text.Trim()),
                                            new SqlParameter("@LSTNUM",txtVat.Text.Trim()),
                                            new SqlParameter("@LSTDATE",LstDate),
                                            new SqlParameter("@CSTNUM",txtDeliveryInterState.Text.Trim()),
                                            new SqlParameter("@CSTDATE",CSTDate),
                                            new SqlParameter("@CEPANUM",txtDeliveryCENVAT.Text.Trim()),
                                            new SqlParameter("@CEPADATE",CepDate),
                                            new SqlParameter("@ENAME",Session["username"].ToString())
                                        };
                 du.executeprocedure("usp_update_ACC_LedgerDeliveryAddressDetails", sparam);
                 lblMsg.Text = "Update Sucessfully";
                 lblMsg.Attributes.Remove("class");
                 lblMsg.Attributes.Add("class", "True");
            }
          
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
        }
    }


    protected void BindLedgerDeliveryAddress(string addressid)
    {
        
            SqlParameter[] sparam ={
                                       new SqlParameter("@DELIVERYADDID",addressid)
                                   };
            DataTable DTDelAddress = du.getdatausingprocedure("USP_ACC_LedgerDeliveryAddressDetails", sparam);
            if (DTDelAddress.Rows.Count > 0)
            {
                hidadd.Value = addressid;
                //DDLDeliveryName.SelectedItem.Text = DTDelAddress.Rows[0]["TITLE"].ToString();
                txtDeliveryName.Text = DTDelAddress.Rows[0]["CONTACTNAME"].ToString();
                txtDileveryAddress1.Text = DTDelAddress.Rows[0]["ADD1"].ToString();
                txtDileveryAddress2.Text = DTDelAddress.Rows[0]["ADD2"].ToString();
                txtDileveryAddress3.Text = DTDelAddress.Rows[0]["ADD3"].ToString();
                txtDileveryAddress4.Text = DTDelAddress.Rows[0]["ADD4"].ToString();
                txtDileveryPincode.Text = DTDelAddress.Rows[0]["PINCODE"].ToString();
               // DDLDileveryCountry.SelectedItem.Text = DTDelAddress.Rows[0]["COUNTRY"].ToString();

                txtVat.Text = DTDelAddress.Rows[0]["LSTNUM"].ToString();
                txtDeliveryVatDate.Text = string.Format("{0:dd-MM-yyyy}", Convert.ToDateTime(DTDelAddress.Rows[0]["LSTDATE"].ToString()));
                txtDeliveryInterState.Text = DTDelAddress.Rows[0]["CSTNUM"].ToString();
                txtDeliveryInterStateDate.Text = string.Format("{0:dd-MM-yyyy}", Convert.ToDateTime(DTDelAddress.Rows[0]["CSTDATE"].ToString()));
                txtDeliveryCENVAT.Text = DTDelAddress.Rows[0]["CEPANUM"].ToString();
                txtDeliveryCENVATDate.Text = string.Format("{0:dd-MM-yyyy}", Convert.ToDateTime(DTDelAddress.Rows[0]["CEPADATE"].ToString()));

                string strsql = "Select STATENAME From MAS_State Where STATEID='" + DTDelAddress.Rows[0]["STATEID"].ToString() + "'";
                DataTable DTState = du.getdata(strsql);
                if (DTState.Rows.Count > 0)
                {
                    DDLDileveryState.SelectedItem.Text = DTState.Rows[0]["STATENAME"].ToString();
                }
            }
        }
  

    protected void grddelivery_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grddelivery.PageIndex = e.NewPageIndex;
        BindGridview();
    }
    protected void grddelivery_SelectedIndexChanged(object sender, EventArgs e)
    {
        string key = grddelivery.SelectedDataKey.Value.ToString();
        Paneltable.Visible = true;
        PanelGrd.Visible = false;
        BindLedgerDeliveryAddress(key);
        btnUpdate.Visible = true;
        btnSave.Visible = false;
        //Response.Redirect("DeliveryAddress.aspx?lid=" + key);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Savedeliveryadress();
        BindGridview();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        UpdateDeliveryDetails();
        BindGridview();
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        PanelGrd.Visible = false;
        Paneltable.Visible = true;
        EmptyTextBoxes(Paneltable);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
       PanelGrd.Visible = true;
       Paneltable.Visible = false;
    }
   
    private void EmptyTextBoxes(Control parent)
    {
        // Loop through all the controls on the page
        foreach (Control c in parent.Controls)
        {
            // Check and see if it's a textbox
            if ((c.GetType() == typeof(TextBox)))
            {
                // Since its a textbox clear out the text  
                ((TextBox)(c)).Text = "";
            }
           
            // Now we need to call itself (recursive) because
            // all items (Panel, GroupBox, etc) is a container
            // so we need to check all containers for any
            // textboxes so we can clear them
            if (c.HasControls())
            {
                EmptyTextBoxes(c);
            }
        }
    }


}
